package com.xasz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xasz.entity.Activity;
import com.xasz.entity.Candidate;
import com.xasz.entity.Jury;
import com.xasz.mapper.ActivityMapper;
import com.xasz.mapper.CandidateMapper;
import com.xasz.mapper.JuryMapper;
import com.xasz.service.ICandidateService;
import com.xasz.service.ex.*;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cai
 * @since 2022-07-28
 */
@AllArgsConstructor
@Service
public class CandidateServiceImpl extends ServiceImpl<CandidateMapper, Candidate> implements ICandidateService {

    ActivityMapper activityMapper;
    CandidateMapper candidateMapper;
    JuryMapper juryMapper;

    @Override
    public List<Candidate> getCandidate(Integer Aid, Integer Uid,String sessionUsername) {
        List<Candidate> candidateList;
        //参与者可查询
        if (juryMapper.selectList(new QueryWrapper<Jury>().eq("activity_id",Aid).eq("name",sessionUsername))==null
                && !activityMapper.selectOne(new QueryWrapper<Activity>().eq("id",Aid)).getOwner().equals(Uid)) {
            throw new DeleteException("你没有权限查看该活动候选人信息");
        }
        QueryWrapper<Candidate> queryWrapper = new QueryWrapper<Candidate>().eq("activity_id",Aid);
        candidateList=candidateMapper.selectList(queryWrapper);
        return candidateList;
    }

    @Override
    public void addCandidate(Candidate candidate,Integer Uid) {
        //拥有者权限判断
        if (!activityMapper.selectOne(new QueryWrapper<Activity>()
                .eq("id",candidate.getActivityId())).getOwner()
                .equals(Uid)) {
            throw new DeleteException("你没有权限添加候选人");
        }
        //判断是否有重复候选人
        QueryWrapper<Candidate> wrapper=new QueryWrapper<Candidate>()
                .eq("activity_id",candidate.getActivityId())
                .eq("name",candidate.getName());
        if (candidateMapper.selectOne(wrapper)!=null){
            throw new RepeatException("已经有重复候选人");
        }
        if (candidateMapper.insert(candidate)!=1){
            throw new CreatException("加入候选人时出现错误");
        }
    }

    @Override
    public void deleteCandidate(Candidate candidate,Integer Uid) {
        //拥有者权限判断
        if (!activityMapper.selectOne(new QueryWrapper<Activity>()
                .eq("id",candidate.getActivityId())).getOwner()
                .equals(Uid)){
            throw new DeleteException("你没有权限删除候选人");
        }
        //判断是有候选人
        QueryWrapper<Candidate> wrapper=new QueryWrapper<Candidate>()
                .eq("activity_id",candidate.getActivityId())
                .eq("name",candidate.getName());
        if (candidateMapper.selectOne(wrapper)==null){
            throw new NotNxistException("候选人不存在！");
        }
        //删除候选人
        if (candidateMapper.delete(wrapper)!=1){
            throw new DeleteException("删除候选人时出现错误");
        }
    }

    @Override
    public void upDataCandidate(Candidate candidate,Integer Uid) {
        //拥有者权限判断
        if (!activityMapper.selectOne(new QueryWrapper<Activity>()
                .eq("id",candidate.getActivityId())).getOwner()
                .equals(Uid)) {
            throw new DeleteException("你没有权限修改候选人");
        }
        //判断是否有重复候选人
        QueryWrapper<Candidate> wrapper=new QueryWrapper<Candidate>()
                .eq("activity_id",candidate.getActivityId())
                .eq("name",candidate.getName());
        if (candidateMapper.selectOne(wrapper)!=null){
            throw new RepeatException("已经有重复候选人");
        }
        //修改候选人信息
        if (candidateMapper.updateById(candidate)!=1){
            throw new UpdateException("修改候选人时出现错误");
        }
    }

}
